<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: pl125 <xskjs888@163.com>
// +----------------------------------------------------------------------

namespace app\app\controller;

use cmf\controller\AdminBaseController;
use think\Db;
use api\wxapp\model\User;
use api\wxapp\model\Cash;
class CashController extends AdminBaseController
{

    public function index(Cash $cash)
    {
        $shop_id = 0;
        $params = $this->param();
        $where = array();
        $sessionAdminId = session('ADMIN_ID');
        if($sessionAdminId !== 1){
            //说明是店铺登陆
            $id = Shop::where('admin_id',$sessionAdminId)->value('id');
            $shop_id = $id;
            $where[] = ['t2.shop_id','eq',$id];
        }
        if(isset($params['nickname'])){
            $where[] = ['t2.nickname','like','%'.$params['nickname'].'%'];
        }
        $query = Cash::alias('t1');
        $query = $cash->leftuser($query);
        $query = $cash->returnfield($query);
        $list = $query->where($where)->paginate(10);
        $this->assign('shop_id',$shop_id);
        $this->assign('list',$list);
        $list->appends($params);  //分页条件
        $this->assign('page', $list->render());
        return $this->fetch();
    }
    public function edit()
    {
        $params = $this->param();
        if($params['status'] == 2){
            //通过审核 天机余额记录
            $info = Cash::get($params['id']);
            $data['balance'] = $info['price'];
            $data['uid'] = $info['uid'];
            $data['type'] = 2;//支出
            $data['remark'] = '余额提现';
            $data['addtime'] = date('Y-m-d H:i:s',time());
            Db::startTrans();
            try{
                //添加余额记录
                $res1 = db::name('app_balance')->insert($data);
                if(!$res1){
                    throw new \Exception('余额记录添加失败');
                }
                if($info['price'] > User::where('id',$info['uid'])->value('balance')){
                    throw new \Exception('余额不足');
                }
                //减少用户余额
                $res2 = User::where('id',$info['uid'])->setDec('balance',$info['price']);

                if(!$res2){
                    throw new \Exception('用户余额扣除失败');
                }
                $res3 = Cash::update($params);
                if(!$res3){
                    throw new \Exception('审核成功');
                }
                Db::commit();
            }catch(\Exception $e){
                Db::rollback(); //回滚事务
                $this->error($e->getMessage());
            }
            $this->success('成功');
        }else{
            $res = Cash::update($params);
            if($res){
                $this->success('成功');
            }else{
                $this->error('失败');
            }
        }
    }

    public function lst(Cash $cash)
    {
        $params = $this->param();
        $where = array();
        $sessionAdminId = session('ADMIN_ID');
        if($sessionAdminId !== 1){
            //说明是店铺登陆
            $id = Shop::where('admin_id',$sessionAdminId)->value('id');
            $where[] = ['t2.shop_id','eq',$id];
        }
        if(isset($params['nickname'])){
            $where[] = ['t2.nickname','like','%'.$params['nickname'].'%'];
        }
        $query = db::name('app_balance')->alias('t1');
        $query = $cash->leftuser($query);
        $query = $cash->returnfield($query);
        $list = $query->where($where)->paginate(10);
        $this->assign('list',$list);
        $list->appends($params);  //分页条件
        $this->assign('page', $list->render());
        return $this->fetch();
    }
}
